package com.module.controller;

import com.module.framework.domain.ResponseResult;
import com.module.framework.domain.User;
import com.module.framework.enums.AppHttpCodeEnum;
import com.module.framework.exception.SystemException;
import com.module.framework.service.LoginService;
import com.module.framework.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Author JiHuiQin
 * @Date 2022/2/16 22:06
 * @Version 1.0
 */

@RestController
@RequestMapping("/admin/user")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    public ResponseResult login(@RequestBody User user) {
        if (!StringUtils.hasText(user.getUserName())) {
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        UserLoginVO userLoginVO = loginService.login(user);
        return ResponseResult.okResult(userLoginVO);
    }

    @GetMapping("/logout")
    public ResponseResult logout() {
        loginService.logout();
        return ResponseResult.okResult();
    }

    @PostMapping("/register")
    public ResponseResult register(@RequestBody User user) {
        String register = loginService.register(user);
        return ResponseResult.okResult(register);
    }

}
